<?php
class AppController extends Controller{
	var $view = 'Theme';
    var $theme = HOST;
	
    var $transaction;
    
	var $uses = array('Cm','CmsSection','User');
	var $permissions = array(
//		'display' => array('admin','manag','emplo','users','firms','copyr')	
	);
	var $helpers = array('Session','Html','Text','Form','Time','Tree','Ajax','Javascript',
						'Js' => array('Jquery'),'Cksource','Section','Number','Lex',
						'FileUpload.FileUpload','Google',	'Footpanel');
    var $components = array( 'Auth','Session','RequestHandler','Email','Cookie','CompCms',
    					'FileUpload.FileUpload','Search.Prg','Gallery.CGallery'
    						);
    
    function beforeRender(){
    	parent::beforeRender();
    	// for ckfinder
		$this->Session->write('HOST', HOST);
    	//read cms settings for page
		$this->CompCms->get();	
    }
	
    /**
     * (non-PHPdoc)
     * @see cake/libs/controller/Controller::beforeFilter()
     */
    function beforeFilter() {
		parent::beforeFilter();	
		
		$this->_setLanguage();       

		$this->Auth->authorize = 'controller';
        $this->Auth->loginAction = array('plugin'=>null,'controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('plugin'=>null,'controller' => 'pages', 'action' => 'display');
        $this->Auth->logoutRedirect = array('plugin'=>null,'controller' => 'users', 'action' => 'login');
		$this->Auth->loginError = __("Your username and/or password was not recognized. Use the form below in order to retrieve your login details.",true);
   	 	$this->Auth->authError = __("You don't have permission to access {$this->params['url']['url']} on this server.",true);
        
//   	 	if(!$this->Auth->user()) 
        if($this->isAuthorized()) $this->Auth->allow($this->action);
        $this->transaction = ConnectionManager::getDataSource('default');
	}
    
	function _setLanguage() {
		if(!isset($this->params['language'])){
			if ($this->Session->read('Config.language') != false && $this->Session->read('Config.language') != '') {     
				Configure::write('Config.language', $this->Session->read('Config.language'));				
			}else{
				// domyślny język
				Configure::write('Config.language', Configure::read('Config.default_lang'));
			}
		}else if(isset($this->params['language']) && $this->params['language'] != Configure::read('Config.language')){
			Configure::write('Config.language', $this->params['language']);
			$this->Session->write('Config.language',$this->params['language']);			
		}else{			
			// domyślny język
		    Configure::write('Config.language', Configure::read('Config.default_lang'));
		}
	}
	
    function isAuthorized(){ 
        if($this->Auth->user('group') == 'admin') return true; //Remove this line if you don't want admins to have access to everything by default 
        if(!empty($this->permissions[$this->action])){ 
           	if($this->permissions[$this->action] == '*') return true;            	
            if(in_array($this->Auth->user('group'), $this->permissions[$this->action])) return true;            
        }else{ 
		    // If there are no permissions, let everyone in 		    
		    $this->Auth->allow($this->action); 
		    return true; 
		}
		$this->Session->setFlash($this->Auth->authError);
		
		if($this->Session->check('Auth.User.id')){
			$this->redirect($this->referer());
		}else{
			$this->redirect($this->Auth->loginAction);
		}
        return false;         
    }
    
    function sendEmail(){}
    
    function &getModel($name = null) {
    	$model = null;
    	if (!$name) {
    		$name = null;
    	}
    	if($name){
    		if (PHP5) {
    			$model = ClassRegistry::init($name);
    		} else {
    			$model =& ClassRegistry::init($name);
    		}
    
    		if (empty($model)) {
    			//				$this->_error('FileUpload::getModel() - Model is not set or could not be found');
    			return null;
    		}
    	}
    	return $model;
    }
    
    
}